﻿using LEDStripMixer.Shared.Constants;
using LEDStripMixer.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LEDStripMixer.Shared.Extensions;

namespace LEDStripMixer.Views.Configuration
{
    public class ConfigurationPresenter
    {
        public IConfiguration View { get; set; }

        public ConfigurationPresenter(IConfiguration view)
        {
            View = view;
        }

        public void Initialise() 
        {
            LoadConfig();
        }

        public void LoadConfig()
        {
            var model = new ConfigurationLookups()
            {
                BaudRates = LookupConstants.BaudRates,
                COMPorts = ArduinoEntryPoint.COMPorts,
                FPS = Enumerable.Range(1, 30).ToList()
            };
            View.BindConfigurationLookups(model);

            var historyModel = new ConfigurationBindModel()
            {
                BaudRate = GlobalSettings.BaudRate,
                COMPort = GlobalSettings.COMPort,
                FPS = GlobalSettings.FPS
            };
            if (model.IsValid()) View.BindConfiguration(historyModel);
        }

        public void SaveConfig(ConfigurationBindModel model) 
        {
            GlobalSettings.BaudRate = model.BaudRate;
            GlobalSettings.COMPort = model.COMPort;
            GlobalSettings.FPS = model.FPS;

            ArduinoEntryPoint.Configure(model.BaudRate, model.COMPort);

            View.Hide();
        }
    }
}
